<?php


namespace app\http\middleware;


use app\admin\jwt\Jwts;
use think\facade\Log;

class Admin
{
    public function handle($request, \Closure $next)
    {
        /**
         * 验证token
         */
        $header = $request->header();
        if(!isset($header['authorization'])){
            return json(['data'=>[],'code'=>401],200);
        }
        Log::error($header);
        $tokendata = explode(' ',$header['authorization']);
        $token = $tokendata[1];
        $arr = Jwts::verifyToken($token);
        if(empty($arr)){
            return jsonp_error(401,'认证失败！');
        }
        //设置登录信息
        session('admin',$arr['data']->userid);
        return $next($request);
    }
}